﻿using SmartWMS.Common.Const;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;
using System.Security.Cryptography.Pkcs;

namespace SmartWMS.API.Extensions
{
    /// <summary>
    /// 
    /// </summary>
    public static class SwaggerServiceRegister
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceCollection"></param>
        /// <returns></returns>
        public static IServiceCollection SwaggerRegister(this IServiceCollection serviceCollection)
        {
            serviceCollection.AddSwaggerGen(option =>
            {
                option.SwaggerDoc("XTong", new OpenApiInfo { Version = "v1", Title = "系统设置", Description = "系统设置" });
                option.SwaggerDoc("BasicsDatum", new OpenApiInfo { Version = "v2", Title = "基础数据", Description = "基础数据" });
                option.SwaggerDoc("Business", new OpenApiInfo { Version = "v3", Title = "业务管理", Description = "业务管理" });
                option.SwaggerDoc("Statement", new OpenApiInfo { Version = "v4", Title = "报表管理", Description = "报表管理" });
                option.SwaggerDoc("Warehouse", new OpenApiInfo { Version = "v5", Title = "仓库管理", Description = "仓库管理" });
                option.SwaggerDoc("Finance", new OpenApiInfo { Version = "v6", Title = "财务管理", Description = "财务管理" });

				//如果需要显示控制器注释只需将第二个参数设置为true
				var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                option.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename), true);

                option.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme()
                {
                    Description = "需要输入token",
                    Name = AssemblyConst.AUTHORIZATION,
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = JwtBearerDefaults.AuthenticationScheme
                });
                option.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                      {
                          new OpenApiSecurityScheme {
                            Reference = new OpenApiReference{
                                Type = ReferenceType.SecurityScheme,
                                Id = JwtBearerDefaults.AuthenticationScheme
                            }
                        },
                        new string[]{}
                      }
                });
            });

            return serviceCollection;
        }
    }
}
